﻿var status = 0;
var giftContent = Array(
	Array("老公老婆戒指LV13", 22, Array(
		Array(1112459,1),
		Array(1112458,-1),
		Array(4001465,-22)
	))
);
var giftId = -1;
var gifts = null;
var price = 999;
function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (status == 0 && mode == 0) {
        cm.dispose();
        return;
    }
    if (mode == 1) {
        status++;
    } else {
        status--;
    }
    if (status == 0) {
		var text = "";
		text += "#e#b选择你要升级的戒指！\r\n";
		for(var key in giftContent) {
			text+="#b#L"+key+"#升级【#r#e"+giftContent[key][0]+"#n#b】 #v1112458# 1个 #v4001465##e#d"+" "+giftContent[key][1]+"个#n#b#l#k\r\n";
		}
		cm.sendSimple(text);
	} else if (status == 1) {
		giftId = parseInt(selection);
		price = giftContent[giftId][1];
		gifts = giftContent[giftId][2];
		var text="#r#e"+giftContent[giftId][0]+"#n#b内容：\r\n";
		for(var key in gifts) {
			var itemId = gifts[key][0];
			var itemQuantity = gifts[key][1];
			text+="#i"+itemId+":##b#z"+itemId+"##k #rx "+itemQuantity+"#k\r\n";
		}
		text+="\r\n#d是否使用#e#r"+price+"#n#d爱心宝石升级老公老婆戒指？#k";
		cm.sendYesNo(text);
	} else if (status == 2) {
		if (giftId!=-1 && gifts != null) {
			if (cm.getSpace(1) < 2 || cm.getSpace(2) < 2 || cm.getSpace(3) < 2 || cm.getSpace(4) < 2 || cm.getSpace(5) < 2) {
				cm.sendOk("您的背包空间不足，请保证每个栏位至少2格的空间，以避免领取失败。");
				cm.dispose();
				return ;
			}
			if (!cm.haveItem(1112458, 1)||!cm.haveItem(4001465, price)) {
				cm.sendOk("您的材料不足，请先凑齐后再兑换。");
				cm.dispose();
				return ;
			}
			for(var key in gifts) {
				var itemId = gifts[key][0];
				var itemQuantity = gifts[key][1];
				cm.gainItem(itemId, itemQuantity);
			}
			cm.gainItem(1112446, -0);
			cm.gainItem(4001465, -0);
			cm.sendOk("恭喜您，兑换成功！");
			cm.dispose();
		} else {
			cm.sendOk("兑换错误！请联系管理员！");
			cm.dispose();
		}
	}
}